package zhangqi.com.fragment_demo;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private Fragment03 fg3;
    private EditText et_main;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //设置默认打开页面是fragment03.xml
        fg3 = new Fragment03();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl,fg3);
        ft.commit();

    }
    public void click01(View view) {
        //把fragment01的界面显示至帧布局中
        //创建fragment对象
        Fragment01 fg1 = new Fragment01();
        //获取fragment管理器
        FragmentManager fm = getFragmentManager();
        //打开事务
        FragmentTransaction ft = fm.beginTransaction();
        //把内容显示至帧布局
        ft.replace(R.id.fl, fg1);
        //提交事务
        ft.commit();
    }
    public void click02(View view) {
        Fragment02 fg2 = new Fragment02();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl,fg2);
        ft.commit();
    }
    public void click03(View view) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl, fg3);
        ft.commit();
    }

    public void click04(View view) {
        et_main = (EditText) findViewById(R.id.et_main);
        String text = et_main.getText().toString();
        Log.i("zhangqi", "文本的内容是----------------------" + text);
        //传递数据
        fg3.setFgText3(text);
    }
    //设置fragment01文本框内容的方法，给Fragment01调用
    public void setFgText1(String text){
        et_main = (EditText) findViewById(R.id.et_main);
        et_main.setText(text);
    }

}
